/**
 * 
 */
package com.pxingg.equipment.powerswitch.Sevytec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.pxingg.datetime.TimeUtils;
import com.pxingg.exception.XException;
import com.pxingg.fep.serial.Serial;

/**
 * @author pxingg
 * 
 */
public class SerialRelay12Test
{
    /**
     * The log of this class
     */
    static Log log = LogFactory.getLog(SerialRelay12Test.class);

    SerialRelay12 switchRelay;

    /**
     * The serial the switch connected
     */
    Serial serial = new Serial("RS485Bus");

    /**
     * The port connect to test
     */
    static final String testPort = "COM1";

    /**
     * The speed in the test
     */
    static final int testSpeed = 9600;

    /**
     * The equipment of the switch
     */
    static final int switchNo = 0x02;

    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @AfterClass
    public static void tearDownAfterClass() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception
    {
    }

    /**
     * Test method for {@link com.pxingg.equipment.powerswitch.Sevytec.SerialRelay12#queryADC()} .
     */
    @Test
    public final void testQueryADC()
    {
        if (!serial.isConnected())
        {
            Assert.assertTrue(serial.connect(SerialRelay12Test.testPort, SerialRelay12Test.testSpeed));
        }
        switchRelay = new SerialRelay12(SerialRelay12Test.switchNo, serial);

        try
        {
            switchRelay.connect();

            switchRelay.queryADC();

            System.out.println("ADC1 = " + switchRelay.getADCInputStatus()[0]);
            System.out.println("ADC2 = " + switchRelay.getADCInputStatus()[1]);
        }
        catch (final XException e)
        {
            Assert.fail(e.getMessage());
        }
        finally
        {
            serial.disconnect();
        }
    }

    /**
     * Test method for {@link com.pxingg.equipment.powerswitch.Sevytec.SerialRelay12#SerialRelay12(byte, com.pxingg.fep.serial.Serial)} .
     */
    @Test
    public final void testSerialRelay12()
    {
        if (!serial.isConnected())
        {
            Assert.assertTrue(serial.connect(SerialRelay12Test.testPort, SerialRelay12Test.testSpeed));
        }
        switchRelay = new SerialRelay12(SerialRelay12Test.switchNo, serial);
        Assert.assertEquals(SerialRelay12.TOTAL_RELAY, switchRelay.getSwitchStatus().length);
        Assert.assertEquals(SerialRelay12.TOTAL_INPUT, switchRelay.getInputStatus().length);
        Assert.assertEquals(SerialRelay12.TOTAL_ADC_INPUT, switchRelay.getADCInputStatus().length);

        for (int i = 1; i < 256; i++)
        {
            switchRelay = new SerialRelay12(i, serial);
            try
            {
                System.out.println("on equiment " + i + " has board and version is " + switchRelay.queryVersion());
                break;
            }
            catch (final XException e)
            {
                System.out.println(e.getMessage());
            }

        }

        serial.disconnect();
    }

    /**
     * Test method for {@link com.pxingg.equipment.powerswitch.Sevytec.SerialRelay12#switchOff(int)} .
     */
    @Test
    public final void testSwitchOff()
    {
        if (!serial.isConnected())
        {
            Assert.assertTrue(serial.connect(SerialRelay12Test.testPort, SerialRelay12Test.testSpeed));
        }
        switchRelay = new SerialRelay12(SerialRelay12Test.switchNo, serial);

        try
        {
            switchRelay.connect();

            // check the status of switch
            final int[] expected = {
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
            };
            switchRelay.switchAllOn();
            switchRelay.queryStatus();
            Assert.assertArrayEquals(expected, switchRelay.getSwitchStatus());
            TimeUtils.mySleep(2000);

            for (int i = 1; i <= SerialRelay12.TOTAL_RELAY; i++)
            {
                switchRelay.switchOff(i);
                TimeUtils.mySleep(2000);
            }
            final int[] expected2 = {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            };
            switchRelay.queryStatus();
            Assert.assertArrayEquals(expected2, switchRelay.getSwitchStatus());
        }
        catch (final XException e)
        {
            Assert.fail(e.getMessage());
        }
        finally
        {
            serial.disconnect();
        }
    }

    /**
     * Test method for {@link com.pxingg.equipment.powerswitch.Sevytec.SerialRelay12#switchOn(int)} .
     */
    @Test
    public final void testSwitchOn()
    {
        if (!serial.isConnected())
        {
            Assert.assertTrue(serial.connect(SerialRelay12Test.testPort, SerialRelay12Test.testSpeed));
        }
        switchRelay = new SerialRelay12(SerialRelay12Test.switchNo, serial);

        try
        {
            switchRelay.connect();

            // set all switch on
            for (int i = 1; i <= SerialRelay12.TOTAL_RELAY; i++)
            {
                switchRelay.switchOn(i);

                TimeUtils.mySleep(2000);
            }
            // check the status of switch
            final int[] expected = {
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
            };
            switchRelay.queryStatus();
            Assert.assertArrayEquals(expected, switchRelay.getSwitchStatus());

            switchRelay.switchAllOff();
            final int[] expected2 = {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            };
            switchRelay.queryStatus();
            Assert.assertArrayEquals(expected2, switchRelay.getSwitchStatus());
            TimeUtils.mySleep(2000);
        }
        catch (final XException e)
        {
            Assert.fail(e.getMessage());
        }
        finally
        {
            serial.disconnect();
        }
    }
}
